﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using NewLife.Log;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TK.Socket
{
    /// <summary>
    /// SocketServerExtensions
    /// </summary>
    public static class SocketServerExtensions
    {
        /// <summary>
        /// 增加socket服务,
        /// 默认监听端口*:65001，tcp udp
        /// 若是tcp协议，应用程数据协议 lv: length:固定2字节代表数据长度,value:数据
        /// </summary>
        public static SocketBuilder AddSocketService(this IServiceCollection services)
        {
            AddSocket(services);
            return new SocketBuilder { ServiceCollection = services };
        }

        /// <summary>
        /// 增加socket服务
        /// 默认监听端口*:65001，tcp udp
        /// 若是tcp协议，应用程数据协议 lv: length:固定2字节代表数据长度,value:数据
        /// </summary>
        public static SocketBuilder AddSocketService(this IServiceCollection services, IConfiguration configuration = null)
        {
            if (configuration != null)
            {
                services.Configure<SocketConfig>(configuration);
            }

            AddSocket(services);
            return new SocketBuilder { ServiceCollection = services };
        }

        /// <summary>
        /// 增加socket服务
        /// 默认监听端口*:65001，tcp udp
        /// 若是tcp协议，应用程数据协议 lv: length:固定2字节代表数据长度,value:数据 
        /// </summary>
        public static SocketBuilder AddSocketService(this IServiceCollection services, Action<SocketConfig> socketConfig = null)
        {
            if (socketConfig != null)
            {
                services.Configure<SocketConfig>(socketConfig);
            }

            AddSocket(services);
            return new SocketBuilder { ServiceCollection = services };
        }

        /// <summary>
        /// 增加数据处理程序，假如启用的粘包处理，接收到的为完整包数据 
        /// </summary>
        /// <typeparam name="THandler">IReceiveDataHandler</typeparam>
        /// <param name="services">IServiceCollection</param>
        public static IServiceCollection AddSokcetDataHandler<THandler>(this IServiceCollection services) where THandler : class, IReceiveDataHandler
        {
            services.TryAddSingleton<IReceiveDataHandler, THandler>();
            return services;
        }

        /// <summary>
        /// 增加数据处理程序，假如启用的粘包处理，接收到的为完整包数据 
        /// </summary>
        /// <typeparam name="THandler">IReceiveDataHandler</typeparam>
        /// <param name="services">IServiceCollection</param>
        public static void AddSokcetDataHandler<THandler>(this SocketBuilder socketBuilder) where THandler : class, IReceiveDataHandler
        {
            socketBuilder.ServiceCollection.TryAddSingleton<IReceiveDataHandler, THandler>();
        }

        private static void AddSocket (IServiceCollection services)
        {
            services.TryAddSingleton<ILog, AppServerLog>();
            services.TryAddSingleton<ISocketServer, SocketServer>();
            services.AddHostedService<SocketHostedService>();
        }
    }

    /// <summary>
    /// socket host servicer
    /// </summary>
    internal class SocketHostedService : BackgroundService
    {
        private readonly ISocketServer _socketServer;

        /// <summary>
        /// ctor
        /// </summary>
        public SocketHostedService(ISocketServer socketServer)
        {
            _socketServer = socketServer;
        }
        /// <summary>
        /// execute
        /// </summary>
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            if (!stoppingToken.IsCancellationRequested)
            {
                await _socketServer.Start();
            }
        }
    }
}
